package com.fankui.serviceedu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fankui.commonutils.JwtUtils;
import com.fankui.commonutils.R;
import com.fankui.serviceedu.client.UcenterClient;
import com.fankui.serviceedu.entity.EduComment;
import com.fankui.serviceedu.service.EduCommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author 范奎
 * @since 2023-12-11
 */
@Api(description = "课程评论管理")
@RestController
@RequestMapping("/eduservice/comment")
public class EduCommentController {
    @Autowired
    private EduCommentService commentService;
    @Autowired
    private UcenterClient ucenterClient;

    @ApiOperation(value = "根据课程id查询评论分页列表")
    @GetMapping("getCommentPageList/{current}/{size}")
    public R getCommentPageList(@ApiParam(value = "current", name = "当前页", required = true) @PathVariable long current,
                                @ApiParam(value = "size", name = "每页记录数", required = true) @PathVariable long size,
                                @ApiParam(value = "courseId", name = "课程id", required = true) String courseId) {
        Page<EduComment> commentPage = new Page<>(current, size);
        QueryWrapper<EduComment> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);
        wrapper.orderByDesc("gmt_create");
        commentService.page(commentPage, wrapper);
        List<EduComment> commentList = commentPage.getRecords();
        Map<String, Object> map = new HashMap<>();
        map.put("items", commentList);
        map.put("current", commentPage.getCurrent());
        map.put("pages", commentPage.getPages());
        map.put("size", commentPage.getSize());
        map.put("total", commentPage.getTotal());
        map.put("hasNext", commentPage.hasNext());
        map.put("hasPrevious", commentPage.hasPrevious());
        return R.ok().data(map);
    }

    @ApiOperation(value = "添加评论")
    @PostMapping("saveComment")
    public R saveComment(@ApiParam(value = "comment", name = "评论对象", required = true) @RequestBody EduComment comment,
                         @ApiParam(value = "request", name = "request对象", required = true) HttpServletRequest request) {
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if (StringUtils.isEmpty(memberId)) {
            return R.error().message("请登录后再进行评论！");
        }
        comment.setMemberId(memberId);
        R r = ucenterClient.getMemberVo(memberId);
        Map<String, Object> memberVo = (Map<String, Object>) r.getData().get("memberVo");
        comment.setNickname((String) memberVo.get("nickname"));
        comment.setAvatar((String) memberVo.get("avatar"));
        boolean save = commentService.save(comment);
        if (save) {
            return R.ok();
        } else {
            return R.error();
        }
    }
}

